﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

//Espacio de nombres añadidos
using System.Activities;
using CrediSoftDemo.WorkFlows;
using Microsoft.Activities.Extensions;

namespace CrediSoftDemo.Controllers
{
    public class IfTestController : Controller
    {
        //
        // GET: /IfTest/

        //Declaramos el workflow de esta manera para una mejor performance

        #region Constants and Fields

        private static readonly IfTest Test1 = new IfTest();
        
        private ActivityInstanceState completionState;

        private WorkflowArguments outputArguments;

        private WorkflowApplication workflowApplication;

        private Exception workflowException;


        #endregion


        #region Public Methods and Operators

        public void IndexAsync()
        {
            // Visual Basic does not support dynamic objects - use ViewData to access the ViewBag

            // Classic way of passing input arguments
            // var input = new Dictionary<string, object> { { "ViewData", this.ViewData } };

            // More "MVC" like method using Microsoft.Activities.WorkflowArguments
            dynamic input = new WorkflowArguments();

            input.ViewData = this.ViewData;
            input.PruebaIf = false;

            this.workflowApplication = new WorkflowApplication(Test1, input)
            {
                Completed = wce => this.AsyncManager.Sync(
                    () =>
                    {
                        this.AsyncManager.OutstandingOperations.Decrement();
                        this.completionState = wce.CompletionState;
                        switch (this.completionState)
                        {
                            case ActivityInstanceState.Closed:
                                this.outputArguments = WorkflowArguments.FromDictionary(wce.Outputs);
                                break;
                            case ActivityInstanceState.Faulted:
                                this.workflowException = wce.TerminationException;
                                break;
                            default:
                                throw new ArgumentOutOfRangeException();
                        }
                    }),
                Aborted = wae => this.AsyncManager.Sync(
                    () =>
                    {
                        this.AsyncManager.OutstandingOperations.Decrement();
                        this.workflowException = wae.Reason;
                    })
            };

            this.AsyncManager.OutstandingOperations.Increment();
            this.workflowApplication.Run();
        }

        public ActionResult Index()
        {
            if (this.workflowException != null)
            {
                throw this.workflowException;
            }

            // ViewBag contains the result set by the workflow
            // See Views / Hellow / Index.cshtml for the related view
            return this.View();
        }

        #endregion
    }
}
